<template>
      <div class='readCardjy'>
        <el-button type="primary" @click="btnClicks">读取</el-button>
       
    </div>
</template>
<script>
export default {
    props:['id'],
    name:'read-cardjy',
    data(){
        return{
            webSocket:null,
            src:"",
            timer:null
        }
    },
    created(){
       
    },
    methods:{
        btnClicks(){
            if(this.webSocket==null)
                this.openStock();
            else{
                this.$message.error('上一次读取还未结束,请不要重复读取');
            }
        },
        openStock(){
            this.webSocket =new WebSocket('ws://localhost:1818');
            let that=this;
            window.onbeforeunload = function() {
               // that.webSocket.send("bStopPlay()");
                //webSocket.send("bStopPlay3()");
            };
            this.webSocket.onerror = function(event) {
                that.onError(event);
                //   alert(event.data);
            };
            
            this.webSocket.onopen = function(event) {
                that.onOpen(event);
                that.start('ReadCard(1001,d:\\)');
                window.setTimeout(function(){
                    try{
                        that.webSocket.close()
                        that.webSocket=null;
                    }catch(e){}
                   
                },2000)
                //webSocket.send('bStartPlay(0)');
                //alert(event.data);
            };
            this.webSocket.onclose = function(event) {
                that.onClose(event);
               // that.webSocket.send("bStopPlay()");
                
                //webSocket.send("bStopPlay4()");
                //   alert(event.data);
            };
            this.webSocket.onmessage = function(event){
                that.onMessage(event)
            }
            
        },
        onError(event){
            this.webSocket=null;
        },
        onOpen(event){
           // this.start('ReadCard(1001,d:\\)');
        },
        onClose(event){
            //this.webSocket.send("bStopPlay()");
            //webSocket.send("bStopPlay4()");
	    },
        onMessage(event) {
            let begin_data="data:image/jpeg;base64,";
            if(event.data.indexOf('BarCodeTransferBegin')>=0){
                //alert(event.data.replace('BarCodeTransferBegin','').replace('BarCodeTransferEnd',''));
            } else if (event.data.indexOf('BeginsGetBase64')>=0) {
               // alert(event.data.replace('BeginsGetBase64','').replace('EndsGetBase64',''));
               // BS64=event.data.replace('BeginsGetBase64','').replace('EndsGetBase64','');
            } else if (event.data.indexOf('BeginGetName')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetName','').replace('EndGetName',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetSex')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetSex','').replace('EndGetSex',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetFolk')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetFolk','').replace('EndGetFolk',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetCode')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginGetCode','').replace('EndGetCode',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetBirthYear')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetBirthYear','').replace('EndGetBirthYear',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetBirthMonth')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
                //alert(event.data.replace('BeginGetBirthMonth','').replace('EndGetBirthMonth',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetBirthDay')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetBirthDay','').replace('EndGetBirthDay',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetAddress')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
              //  alert(event.data.replace('BeginGetAddress','').replace('EndGetAddress',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetAgency')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
              //  alert(event.data.replace('BeginGetAgency','').replace('EndGetAgency',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetValidtotal')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetValidtotal','').replace('EndGetValidtotal',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetValidBegin')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetValidBegin','').replace('EndGetValidBegin',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetValidEnd')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetValidEnd','').replace('EndGetValidEnd',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginReadNewAppMsg')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginReadNewAppMsg','').replace('EndReadNewAppMsg',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetNewAddress')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginGetNewAddress','').replace('EndGetNewAddress',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetPhotobuf')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
              //  alert(event.data.replace('BeginGetPhotobuf','').replace('EndGetPhotobuf',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetJPGPhotobuf')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginGetJPGPhotobuf','').replace('EndGetJPGPhotobuf',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetSAMID')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginGetSAMID','').replace('EndGetSAMID',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetBirthDate')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
              //  alert(event.data.replace('BeginGetBirthDate','').replace('EndGetBirthDate',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginGetFPData')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginGetFPData','').replace('EndGetFPData',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            } else if (event.data.indexOf('BeginbLivingBodyDetect')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginbLivingBodyDetect','').replace('EndbLivingBodyDetect',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            }else if (event.data.indexOf('BeginlImageMatch')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginlImageMatch','').replace('EndlImageMatch',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            }
            else if (event.data.indexOf('BeginBase64Encode')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
              //  alert(event.data.replace('BeginBase64Encode','').replace('EndBase64Encode',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            }
            else if (event.data.indexOf('BeginbDeleteFile')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginbDeleteFile','').replace('EndbDeleteFile',''));
                //alert(begin_data + event.data.substring(0, event.data.length - 11));
            }
                else if (event.data.indexOf('BeginbStartPlay2')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
               // alert(event.data.replace('BeginbStartPlay2','').replace('EndbStartPlay2',''));
                }
            
            else if (event.data.indexOf('BeginbStartPlay')>=0) {
                //document.getElementById("preview").src = begin_data + event.data.substring(0, event.data.length - 11);
             //   alert(event.data.replace('BeginbStartPlay','').replace('EndbStartPlay',''));
              //  alert('2');
                }
            else if(event.data.indexOf("BeginReadCard")>=0){
                let result=event.data.replace("BeginReadCard","").replace("EndReadCard","");
                /*let index=result.indexOf("PhotoB64");
                let data=JSON.parse(result.substring(0,index-2)+"}");
                let img=result.substring(index+11);
                img=img.replace("\"}EndReadCard","")*/
                let data=JSON.parse(result);
              //  console.log(img)
             //   console.log(data)
            //    console.log(this.id)
                let isNew=true;
                let datas={};
                let BirthDate="";
                let imgs="";
                try{
                    BirthDate=data.BirthDate;
                   // alert(BirthDate)
                    if(BirthDate){
                        datas.Born=BirthDate.substring(0,4)+"-"+BirthDate.substring(4,6)+"-"+BirthDate.substring(6);
                
                    }else{
                        let birthyear=data.birthyear;
                        let birthmonth=data.birthmonth;
                        let birthday=data.birthday;

                        if(Number(birthmonth)<10){
                            birthmonth="0"+birthmonth;
                        }
                        if(Number(birthday)<10){
                            birthday="0"+birthday;
                        }
                        datas.Born=birthyear+"-"+birthmonth+"-"+birthday;
                        isNew=false;
                        BirthDate=birthyear+"-"+birthmonth+"-"+birthday;
                    }
                }catch(e){}
                if(isNew){
                    datas.labelA=data.Name;
                    datas.Sex=data.Sex;
                    datas.mz=data.Folk+"族";
                    datas.Address=data.Address;
                    datas.CardNo=data.IdNo;
                    datas.image=begin_data+data.PhotoB64;
                    imgs=data.PhotoB64;
                }else{
                    datas.labelA=data.name;
                    datas.Sex=data.sex;
                    datas.mz=data.nation+"族";
                    datas.Address=data.address;
                    datas.CardNo=data.cardno;
                    datas.image=begin_data+data.photobuf;
                    imgs=data.photobuf;
                }
                
                var date = new Date();
                let curYear=date .getFullYear(); //获取完整的年份(4位)
                let age=Number(curYear)-Number(BirthDate.substring(0,4));
                age=age.toFixed(0);
                datas.age=age;
                let ids=this.id.split("|");
                for(let i=0;i<ids.length;i++){
                    let id_=ids[i].split(":");
                    if(id_[0]!="image"){
                        let data={};
                        let val=datas[id_[0]];
                        data.id=id_[1];
                        data.val=val;
                        
                        this.$emit("input",JSON.stringify(data));
                    }else{
                        this.$post("business","/api/file/fileUploadBase64",{data:imgs,model:"头像",type:"头像",name:"头像"}).then(datas=>{
                               
                               let url=datas[0].url;
                               console.log("uploadImg==========================");
                               console.log(url);
                               let data_={};
                               data_.id=id_[1];
                               data_.val=url;
                               this.$emit("input",JSON.stringify(data_));
                           });
                    }
                    
                }
                this.webSocket.close()
                this.webSocket=null;
            }
            else {
                this.src =begin_data+event.data;
            }
            
                //document.getElementById('photo').src =begin_data+event.data;
            //	document.getElementById('result').value = event.data;
            //	document.getElementById('photo').src =event.data;
            
        },
        start(el) {
        
          /*  if(el=='bStartRec(d:\\test.avi)')
            {
                timer = window.setInterval("TimeGo()",1000);
            }
            if(el=='bStopRec')
            {
                window.clearInterval(timer);
                TotalTimelenth=0;
                alert("录屏结束");
            }*/
            let that=this;
           
            this.webSocket.send(el);
           
            
            return false;
        }
    }
}
</script>